1. Constructors and Inheritance in Solidity

Question:
Write a contract Parent that initializes a public state variable owner to the deployer’s address using a constructor. Then, write a contract Child that inherits from Parent and adds a constructor that accepts an integer age as a parameter and stores it in a public variable age.

Answer:



2. Interacting Between Contracts in Solidity

Question:
Write two contracts, Caller and Callee. In Callee, write a public function multiply(uint x, uint y) that returns the product of x and y. In Caller, write a function callMultiply that calls multiply from Callee and returns the result.

Answer:



3. Errors, Checks, and Conditions in Solidity

Question:
Write a function withdraw(uint amount) inside a contract Bank that allows the caller to withdraw ether only if their balance is greater than or equal to amount. If not, it should revert the transaction with a custom error message “Insufficient balance”.

Answer:



4. Enums, Access Control, and Function Updates in Solidity

Question:
Write a contract Voting where an enum State has values Created, Voting, and Ended. Implement a modifier onlyOwner that restricts access to the contract owner, and use it in the function startVoting which transitions the state from Created to Voting.

Answer:



5. Events in Solidity: Emitting Logs

Question:
Write a contract Token that allows users to transfer tokens. Emit an event Transfer with parameters from, to, and amount whenever a transfer occurs.

Answer:



6. Functions in Solidity: Types and Use Cases

Question:
Write a contract Calculator with two public functions: add(uint a, uint b) and subtract(int a, int b). The add function should return the sum, and the subtract function should return the difference.

Answer:



7. Inheritance in Solidity with Function Overrides

Question:
Write a contract Animal with a virtual function makeSound that returns "Animal sound". Then, create a contract Dog that overrides makeSound and returns "Bark!".

Answer:



8. Access Control: Using onlyOwner Modifier

Question:
Write a contract Store where only the owner can set the price of a product using the setPrice(uint _price) function. Use the onlyOwner modifier to restrict access.

Answer:



9. Reentrancy Guard Example

Question:
Write a contract SafeBank where users can deposit and withdraw Ether. Implement a reentrancy guard on the withdraw function to prevent reentrancy attacks.

Answer:



10. Using Events for Logging Function Calls

Question:
Write a contract Counter that has a public function increment which increases a counter by 1. Emit an event Incremented every time the function is called with the new counter value.

Answer: